package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Warehousing;
import com.zyd.shiro.business.service.PakWarehousingService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakWarehousing;
import com.zyd.shiro.persistence.mapper.PakWarehousingMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakWarehousingServiceImpl implements PakWarehousingService {

    @Autowired
    private PakWarehousingMapper pakWarehousingMapper;

    @Override
    public Warehousing insert(Warehousing warehousing) {
        Assert.notNull(warehousing, "对象不可为空！");
        warehousing.getPakWarehousing().setUnicode("RK"+System.currentTimeMillis());
        warehousing.getPakWarehousing().setUpdateTime(new Date());
        warehousing.getPakWarehousing().setCreateTime(new Date());
        pakWarehousingMapper.insertSelective(warehousing.getPakWarehousing());
        return warehousing;
    }

    @Override
    public void insertList(List<Warehousing> warehousings) {
            for(Warehousing warehousing:warehousings){
               warehousing.getPakWarehousing().setUpdateTime(new Date());
               warehousing.getPakWarehousing().setCreateTime(new Date());
               pakWarehousingMapper.insertSelective(warehousing.getPakWarehousing());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakWarehousingMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Warehousing warehousing) {
        Assert.notNull(warehousing, "对象不可为空！");
        warehousing.setUpdateTime(new Date());
        return pakWarehousingMapper.updateByPrimaryKey(warehousing.getPakWarehousing()) > 0;
    }

    @Override
    public boolean updateSelective(Warehousing warehousing) {
        Assert.notNull(warehousing, "对象不可为空！");
        warehousing.setUpdateTime(new Date());
        return pakWarehousingMapper.updateByPrimaryKeySelective(warehousing.getPakWarehousing()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Warehousing getByPrimaryKey(Long primaryKey) {
        PakWarehousing pakWarehousing = pakWarehousingMapper.selectByPrimaryKey(primaryKey);
        return null == pakWarehousing ? null : new Warehousing(pakWarehousing);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Warehousing getOneByEntity(Warehousing test) {
        Assert.notNull(test, "对象不可为空！");
        PakWarehousing pakWarehousing = pakWarehousingMapper.selectOne(test.getPakWarehousing());
        return null == pakWarehousing ? null : new Warehousing(pakWarehousing);
    }

    @Override
    public List<Warehousing> listAll() {
        List<PakWarehousing> pakWarehousings = pakWarehousingMapper.selectAll();
        if (CollectionUtils.isEmpty(pakWarehousings)) {
            return null;
        }
        List<Warehousing> warehousings = new ArrayList<>();
        for (PakWarehousing pakWarehousing : pakWarehousings) {
            warehousings.add(new Warehousing(pakWarehousing));
        }
        return warehousings;
    }

    @Override
    public List<Warehousing> listByEntity(Warehousing entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Warehousing> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakWarehousing> pakWarehousings = pakWarehousingMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakWarehousings)) {
            return null;
        }
        List<Warehousing> warehousings = new ArrayList<>();
        for (PakWarehousing pakWarehousing : pakWarehousings) {
            warehousings.add(new Warehousing(pakWarehousing));
        }
        PageInfo bean = new PageInfo<PakWarehousing>(pakWarehousings);
        bean.setList(warehousings);
        return bean;
    }
@Override
    public void increase(PakWarehousing pakWarehousing) {
        pakWarehousingMapper.insert(pakWarehousing);
    }




}
